<?php

class DB{
	protected $config = null;
	
	public $link = null;
	
	public function __construct(){
		$this->config = require_once(APP_PATH . "app/Home/Config/db_config.php");
		$this->init();
	}
	
	public function init(){
		if(empty($this->config)){
			throw new Exception("配置文件有问题！请检查app/Home/Config/db_config.php文件。");
		}
		$config = $this->config;
		$this->link = new mysqli();
		$this->link->connect(
			$config['host'],
			$config['user'],
			$config['pass'],
			$config['dbname'],
			$config['port']
		);
		if(mysqli_connect_errno()){
			throw new Exception("数据库链接错误：" . mysqli_connect_error());
		}
	}
	
	public function getLink(){
		return $this->link;
	}
	
	public function __destruct(){
		$this->link->close(); 
	}
	
	public function query($sql_language){
		$this->link->query("set names utf8");
		return $this->link->query($sql_language);
	}
}